package com.open.cloud.rbac.service.orgpost.impl;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.open.cloud.rbac.entity.orgpost.CoreCompany;
import com.open.cloud.rbac.entity.orgpost.CoreOrganization;
import com.open.cloud.rbac.mapper.orgpost.CoreCompanyMapper;
import com.open.cloud.rbac.mapper.orgpost.CoreOrganizationMapper;
import com.open.cloud.rbac.object.orgpost.*;
import com.open.cloud.rbac.service.orgpost.ICoreCompanyService;
import com.open.cloud.rbac.service.orgpost.ICoreOrganizationService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公司表 服务实现类
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Service
public class CoreCompanyServiceImpl extends ServiceImpl<CoreCompanyMapper, CoreCompany>
        implements ICoreCompanyService {

    @Resource
    private CoreCompanyMapper coreCompanyMapper;

    @Resource
    private Snowflake snowflake;

    @Resource
    private CoreOrganizationMapper organizationMapper;

    @Resource
    private ICoreOrganizationService organizationService;

    /**
     * 新增公司信息
     *
     * @param companyDto
     * @return
     */
    @Override
    public JSONObject addCompanyInfo(CoreCompanyDto companyDto) {
        CoreCompany coreCompany = new CoreCompany();
        BeanUtils.copyProperties(companyDto, coreCompany);
        coreCompany.setId(snowflake.nextIdStr());
        coreCompany.setCreateTime(new Date());
        coreCompany.setCreateBy("admin");
        coreCompany.setUpdateTime(new Date());
        coreCompany.setUpdateBy("admin");
        int i = coreCompanyMapper.insert(coreCompany);
        JSONObject result = new JSONObject();
        if (i > 0) {
            result.put("code", 200);
            result.put("message", "创建成功");
        } else {
            result.put("code", 500);
            result.put("message", "创建失败");
        }
        return result;
    }

    /**
     * 编辑公司信息
     *
     * @param companyDto
     * @return
     */
    @Override
    public JSONObject updateCompanyInfo(CoreCompanyDto companyDto) {
        CoreCompany coreCompany = new CoreCompany();
        BeanUtils.copyProperties(companyDto, coreCompany);
        coreCompany.setUpdateTime(new Date());
        coreCompany.setUpdateBy("admin");
        int i = this.baseMapper.updateById(coreCompany);
        JSONObject resultObj = new JSONObject();
        if (i > 0) {
            resultObj.put("code", 200);
            resultObj.put("message", "修改成功");
        } else {
            resultObj.put("code", 500);
            resultObj.put("message", "修改失败");
        }
        return resultObj;
    }

    /**
     * 删除公司信息
     *
     * @param companyId
     * @return
     */
    @Override
    public JSONObject deleteCompanyInfo(String companyId) {
        LambdaQueryWrapper orgQuery = Wrappers.<CoreOrganization>lambdaQuery()
                .eq(CoreOrganization::getCompanyId, companyId);
        List orgList = organizationMapper.selectList(orgQuery);
        JSONObject result = new JSONObject();
        if (orgList.size() > 0) {
            result.put("code", 500);
            result.put("message", "该公司下存在部门组织，无法删除");
        } else {
            int i = this.baseMapper.deleteById(companyId);
            if (i > 0) {
                result.put("code", 200);
                result.put("message", "删除成功");
            } else {
                result.put("code", 500);
                result.put("message", "删除失败");
            }
        }
        return result;
    }

    /**
     * 分页查询公司
     *
     * @param companySelectDto
     * @return
     */
    @Override
    public IPage<CoreCompany> selectCompanyPage(CoreCompanySelectDto companySelectDto) {
        LambdaQueryWrapper companyQuery = Wrappers.<CoreCompany>lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(companySelectDto.getStatus()), CoreCompany::getStatus, companySelectDto.getStatus())
                .orderByDesc(CoreCompany::getSort);
        IPage<CoreCompany> page = new Page<>(companySelectDto.pageNo, companySelectDto.getPageSize());
        IPage data = this.baseMapper.selectPage(page, companyQuery);
        return data;
    }

    /**
     * 查询公司列表
     *
     * @return
     */
    @Override
    public List<CoreCompany> selectCompanyList() {
        LambdaQueryWrapper companyQuery = Wrappers.<CoreCompany>lambdaQuery()
                .eq(CoreCompany::getStatus, "0")
                .orderByDesc(CoreCompany::getSort);
        List data = this.baseMapper.selectList(companyQuery);
        return data;
    }

    /**
     * 查询公司部门树结构
     *
     * @return
     */
    @Override
    public List<CoreCompanyVo> selectCompanyOrgTree() {
        LambdaQueryWrapper companyQuery = Wrappers.<CoreCompany>lambdaQuery()
                .eq(CoreCompany::getStatus, "0")
                .orderByDesc(CoreCompany::getSort);
        List<CoreCompany> data = this.baseMapper.selectList(companyQuery);

        List<CoreCompanyVo> list = new ArrayList<>();
        CoreOrganizationSelectDto orgSelectDto = new CoreOrganizationSelectDto();

        data.forEach(item -> {
            CoreCompanyVo companyVo = new CoreCompanyVo();
            BeanUtils.copyProperties(item, companyVo);
            orgSelectDto.setCompanyId(item.getId());
            List<CoreOrganizationVo> children = organizationService.selectOrganizationTree(orgSelectDto);
            companyVo.setChildren(children);
            list.add(companyVo);
        });
        return list;
    }

    /**
     * 根据companyId获取详情
     *
     * @param companyId
     * @return
     */
    @Override
    public CoreCompany getCompanyDetailById(String companyId) {
        CoreCompany coreCompany = this.baseMapper.selectById(companyId);
        return coreCompany;
    }

}
